package com.yufa.springcloud.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.yufa.springcloud.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class FileConstroller {

    @Autowired
    private EurekaClient eurekaClient;

    @PostMapping(value = "/upload")
    private List<String> upload(HttpServletRequest request) {
        List<String> list = new ArrayList<>();
        List<MultipartFile> files = new ArrayList<>();
        MultipartFile file1 = ((MultipartHttpServletRequest) request).getFile("file");
        if (file1 == null) {
            list.add("为空");
            return list;
        }
        files.add(file1);
        files.forEach(file -> {
            try {
                String ss = file.getName() + file.getOriginalFilename() + file.getContentType() + new String(file.getBytes());
                System.out.println("=====" + ss);
                list.add(ss);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        return list;
    }

    @PostMapping(value = "/get-user")
    private User getUser(@RequestBody User user) {
        System.out.println(user);
        System.out.println(user);
        return user;
    }

    @GetMapping(value = "/eurekainfo")
    private String info() {
        InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("PROVIDER-USER", false);
        return instanceInfo.getHomePageUrl();
    }

}
